﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookManagermentSystem
{
    public partial class RegisterForm : Form
    {
        public RegisterForm()
        {
            InitializeComponent();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            string connStr = "Server=127.0.0.1;Database=csharpbookdb;uid=root;pwd=123456;";

            try
            {
                string User = tbUserName.Text;
                string Pwd = tbPassword.Text;

                using (MySqlConnection conn = new MySqlConnection(connStr))
                {
                    conn.Open();

                    if (tbUserName.Text == null || tbUserName.Text.Length == 0 || tbUserName.Text == " ")
                    {
                        MessageBox.Show("请输入用户名!");
                        return;
                    }

                    if (tbPassword.Text == null || tbPassword.Text.Length == 0 || tbPassword.Text == " ")
                    {
                        MessageBox.Show("请输入密码!");
                        return;
                    }

                    string sql = "insert into login(UserName, Password) values('{0}', '{1}')";

                    sql = string.Format(sql, tbUserName.Text, tbPassword.Text);

                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    int returnValue = cmd.ExecuteNonQuery();

                    if (returnValue != -1)
                    {
                        MessageBox.Show("注册成功");
                        this.Close();
                    }
                    else 
                    {
                        MessageBox.Show("注册失败");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("注册失败," + ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void RegisterForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoginForm.getInstance().Show();
        }
    }
}
